import React, {useState} from "react";
import Card from "../UI/Card/Card";
import "./Logsform.css";

function Logsfrom(props) {
    // let inputDate, inputDesc, inputTime

    const [inputDate,setInputDate] = useState('');
    const [inputDesc,setInputDesc] = useState('');
    const [inputTime,setInputTime] = useState('');
    const dateChangeHandler = (e) => {
        setInputDate(e.target.value);
    };
    const descChangeHandler = (e) => {
        setInputDesc(e.target.value);
    };
    const timeChangeHandler = (e) => {
        setInputTime(e.target.value);
    };
 
    const formSubmitHandler = (e) => {
        e.preventDefault();
        
        const newLog = {
            date: new Date(inputDate),
            desc: inputDesc,
            time: +inputTime
        };
        //上传父组件更新页面
        if(inputDate&&inputDesc&&inputTime){
            props.onSaveLogs(newLog)
        }
        setInputDate('');
        setInputDesc('');
        setInputTime('');
    
        console.log(newLog);

    };

    return (
        <Card className="logs-form">
            <form onSubmit={formSubmitHandler}>
                <div className="form-item">
                    <label htmlFor="date">日期</label>
                    <input
                        onChange={dateChangeHandler}
                        id="date"
                        type="date"
                        value={inputDate}
                    ></input>
                </div>
                <div className="form-item">
                    <label htmlFor="desc">内容</label>
                    <input
                        onChange={descChangeHandler}
                        id="desc"
                        type="text"
                        value={inputDesc}
                    ></input>
                </div>
                <div className="form-item">
                    <label htmlFor="time">时间</label>
                    <input
                        onChange={timeChangeHandler}
                        id="time"
                        type="number"
                        value={inputTime}
                    ></input>
                </div>
                <div className="form-btn">
                    <button>添加</button>
                </div>
            </form>
        </Card>
    );
}

export default Logsfrom;
